package com.qianxun.module.datasource1.develop.rest;

import com.qianxun.module.datasource1.admin.service.SysDataModifyLockService;
import com.qianxun.module.datasource1.develop.service.SysFunctionRoleService;
import com.qianxun.module.datasource1.develop.entity.SysFunctionRole;
import com.qianxun.module.datasource1.develop.dto.SysFunctionRoleDTO;
import com.qianxun.module.datasource1.develop.util.SysFunctionRoleUtil;
import com.qianxun.module.base.constant.Result;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.base.bo.ZTreeNode;
import com.qianxun.module.base.bo.ValidGroup;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
* com.zch.admin.base.entity.SysAdminFunctionRole模型http接口处理
*
* @author: admin
* @date: 2019-11-27
* @version: 1.0.0
*/
@Validated(value = {ValidGroup.All.class})
@RestController
@RequestMapping(value="/develop/functionRole" ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SysFunctionRoleRestController {

    @Resource
    private SysFunctionRoleService baseService;
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;

    @PostMapping(value = {"/add"})
    public Result add(SysFunctionRoleDTO query) {
        baseService.add(query);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"})
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        sysDataModifyLockService.checkLocked(SysFunctionRoleUtil.dataLockKey(id));

        baseService.deleteSelfAndChildrenById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"})
    public Result updateById(@PathVariable(value = "id") Integer id, SysFunctionRoleDTO query) {
        sysDataModifyLockService.checkLocked(SysFunctionRoleUtil.dataLockKey(id));

        SysFunctionRole sysAdminFunctionRole = new SysFunctionRole();
        ObjectUtil.switchAllFieldValue(sysAdminFunctionRole, query);
        baseService.updateById(sysAdminFunctionRole);
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"})
    public Result searchAll() {
        List<SysFunctionRole> list = baseService.list();
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/id/{id}"})
    public Result searchOneById(@PathVariable(value = "id") Long id) {
        SysFunctionRole SysAdminFunctionRole = baseService.getById(id);
        return Result.success(SysAdminFunctionRole);
    }

    @RequestMapping(value = {"/search/tree/{userUuid}"} ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchFunctionRoleTreeByRoleId(@PathVariable(value = "userUuid") String userUuid){
        List<ZTreeNode> list = baseService.searchFunctionRoleTreeByUserUuid(userUuid);
        return ObjectUtil.toJson(list);
    }

}
